package com.calvin.study.multithread.volatiledemo;

import java.util.concurrent.atomic.AtomicInteger;

class Data2 {

	AtomicInteger atomicNumber = new AtomicInteger(5);

	public void addAtomic() {
		// 先获取值再每次+1
		atomicNumber.getAndIncrement();
	}
}

public class VolatileDemo2 {
	public static void main(String[] args) throws InterruptedException {
		Data2 data = new Data2();

		new Thread(new Runnable() {
			@Override
			public void run() {
				data.addAtomic();
				System.out.println(Thread.currentThread().getName() + "\t finally number value:" + data.atomicNumber);
			}
		}, "t1").start();
		


		new Thread(new Runnable() {
			@Override
			public void run() {
				data.addAtomic();
				System.out.println(Thread.currentThread().getName() + "\t finally number value:" + data.atomicNumber);
			}
		},"t2").start();;

	}
}
